package com.bw.flinkstreaming.transform.bw1;

import com.bw.flinkstreaming.source.bw1.NotParallelSource;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.ConnectedStreams;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.co.CoMapFunction;

/**
 * connect和union的区别在于：数据类型是否一致，connect可以不一致
 * */
public class ConnectionTest {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //数据源1
        DataStreamSource<Long> source1 = env.addSource(new NotParallelSource());
        //数据源2
        DataStreamSource<Long> source2 = env.addSource(new NotParallelSource());

        //修改数据源2的类型
        SingleOutputStreamOperator<String> source2_str = source2.map(new MapFunction<Long, String>() {
            @Override
            public String map(Long value) {
                return " " + value;
            }
        });
        //合并后的流
        ConnectedStreams<Long, String> source3 = source1.connect(source2_str);

        /**
         * Long：数据源1的输入类型
         * String：数据源2的输入类型
         * Object：返回的类型
         * */
        SingleOutputStreamOperator<Object> source3_result = source3.map(new CoMapFunction<Long, String, Object>() {
            //处理数据源1
            @Override
            public Object map1(Long value) {
                return value;
            }

            //处理数据源2
            @Override
            public Object map2(String value) {
                return value;
            }
        });
        source3_result.print().setParallelism(1);
        env.execute("connectTest");

    }
}
